 * Source: 
 * Action: estimates the random parameter logit (Table 3) and the conditional logit model in Appendix 
 * Output: ----
 *********************************************************************
 * 
 do "mixlogit.ado"
 do "mixlogit_d1.ado"
 do "mixlpred.ado"


 * load data to use
 use "data_acute_illness_patients_mixedlogit_vs4.dta", clear

 ******************************
 ** RPM UNINSTRUMENTED AND INSTRUMENTED 
 ** Note that: since the distance of informal is imputed to zero we are able to estimate only different intercepts for public and private clinics
 ******************************
  * UNINSTRUMENTED: preliminary step: gets the starting values 
    set seed 545415
	mixlogit d_  dist_dw_hf_2 dist_22  alt2-alt4   , group(choicesit) id(idid) rand( doctor_tot) 
	matrix b48 = e(b)   
 
  * INSTRUMENTED: preliminary step: gets the starting values 
  set seed 545415
  mixlogit d_  dist_dw_hf_2 dist_22   nu_8d alt2-alt4  , group(choicesit) id(idid) rand( doctor_tot) 
  matrix b50 = e(b)
  
  
  ********************************************
  *Table 3: Models of health provider choice
  ********************************************
  * RPM uninstrumented 
   set seed 101
   matrix b47 = b48[1,1..5], 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0,      0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
   mixlogit d_  dist_dw_hf_2 dist_22  alt2-alt4 , group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b47, copy)
  
  * RPM control function
    set seed 101
    matrix b52 = b50[1,1..6], 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0,  0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
    mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq , group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b52, copy) 
    mixlpred PR0, nrep(500)
    est store mixl51
    gen esample = (e(sample)==1)

  
  *******************************************************************
  * ROBUSTNESS ASKED DURING THE REVISION PROCESS
  *******************************************************************
    *Answer to AE: March 2023
	browse id no_facility name_facility nr* water2009 generator2009 powersupply waittime2009
	replace nrmajorsurg = 0 if nrmajorsurg==.
	replace nrminorsurg = 0 if nrminorsurg==.
	gen theatre = (nrmajorsurg>0)
	
	replace waittime2009 = -waittime2009
	replace nomed2009 = -nomed2009

	* using a revised CF with more three additional quality variables
	matrix b53 = b50[1,1..6], 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,   0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
	
	set seed 101
	mixlogit d_  dist_dw_hf_2 dist_22   nu_8g  alt2-alt4  nu_8gsq waittime2009 nomed2009 powersupply, group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b53, copy) 
	mixlpred PR0, nrep(500)
	
	* using the old CF(nu_8d) with three additional quality variables in the demand equation only
	 mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq waittime2009 nomed2009 powersupply, group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b53, copy) 
	 
	* using the old CF and add quality variables one by one 
	matrix b54 = b50[1,1..6], 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0, 0.0,     0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
	mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq waittime2009 , group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b54, copy) 
	est store mixl54_a


	matrix b54 = b50[1,1..6], 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,     0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
	mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq waittime2009 nomed2009, group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b54, copy)
	est store mixl54_b


	matrix b54 = b50[1,1..6], 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,      0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
	mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq waittime2009 nomed2009 powersupply, group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b54, copy)
	est store mixl54_c

	 
	matrix b54 = b50[1,1..6], 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,     0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
	mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq waittime2009 nomed2009 powersupply water2009, group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b54, copy)
	est store mixl54_d

	 
	matrix b54 = b50[1,1..6], 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,    0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
	mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq waittime2009 nomed2009 powersupply water2009 theatre, group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b54, copy)
	est store mixl54_e



 /*
***************************
** OTHER ROBUSTNESS : (a) Assuming log-normal distribution 
** (b) using linear/quadratic distance and (c) various distance measures. 
*****************************
*do "Steb4b_robustness_main_models.do"

*/
    * Table A.6: introduce an additional control variable: distance to ilorin
	matrix b53 = b50[1,1..6], 0.0, 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0,  0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
	mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq distjtoil , group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b53, copy) 

	* Table A.8: introduce tarred road and interaction between tarred road and distance
  gen dist_dw_hf_2_tarred = dist_dw_hf_2*tarred
  gen dist_22_tarred = dist_22*tarred

  matrix b54 = b50[1,1..6], 0.0, 0.0, 0.0, 0.0, 0.003, 0.26, 0.001, 0.0, 0.0, 0.0, 0.0,  0.001, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0
  mixlogit d_  dist_dw_hf_2 dist_22   nu_8d  alt2-alt4  nu_8dsq tarred dist_dw_hf_2_tarred dist_22_tarred, group(choicesit) id(idid) cluster(q01_10) rand(doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4 ) from(b54, copy) 
  est store mixl51d_tarred


